package com.example.demo.pattern.proxy.dynamics;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @program: demo
 * @description: cglib动态代理
 * @author: yby
 * @create: 2020-06-23 16:24
 **/
public class CglibProxy implements MethodInterceptor {

    private Object obj;

    public Object getInstance(final Object obj){
        this.obj = obj;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.obj.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("start....");
        methodProxy.invoke(o,objects);
        //methodProxy.invokeSuper(o,objects);
        System.out.println("end...");
        return null;
    }
}
